package handlers

import (
	"code/config"
	"code/utils"
	"fmt"
	"strings"
)

// DeleteHandler 处理根据给定配置生成删除代码的逻辑
func DeleteHandler(cfg config.Config) config.CodeResult {
	var codes config.CodeResult
	blue.Printf("开始生成 %s 的代码\n", cfg.Action)

	codeController := controllerBuildDelete(cfg)
	codeControllerContent := fmt.Sprintf("//%s\n%s\n//%s\r\n\n", cfg.StartTag, codeController[0], cfg.EndTag)

	codes.ControllerCode = codeControllerContent
	codes.ServiceCode = fmt.Sprintf("//%s\n%s\n//%s\r\n\n", cfg.StartTag, serviceBuildDelete(cfg)[0], cfg.EndTag)
	codes.ServiceImplCode = fmt.Sprintf("//%s\n%s\n//%s\r\n\n", cfg.StartTag, serviceImplBuildDelete(cfg)[0], cfg.EndTag)
	codes.MapperCode = fmt.Sprintf("//%s\n%s\n//%s\r\n\n", cfg.StartTag, mapperBuildDelete(cfg)[0], cfg.EndTag)
	codes.MapperXMLCode = fmt.Sprintf("<!--%s-->\n%s\n<!--%s-->\r\n\n", cfg.StartTag, mapperXMLBuildDelete(cfg)[0], cfg.EndTag)

	green.Printf("生成完毕\n%s\n", cfg.Name)
	return codes
}

// 控制器构建函数 - 删除
func controllerBuildDelete(config config.Config) []string {
	controllerTemplate := `
// 控制器
@{ApiType}Mapping("{Api}/{id}")
public AjaxResult delete{tag}(@PathVariable("id") Long id) {
    return toAjax({service}.delete{Name}{tag}(id));
}
`
	controllerCode := strings.ReplaceAll(controllerTemplate, "{Name}", utils.LineToPascalCase(config.Table))
	controllerCode = strings.ReplaceAll(controllerCode, "{tag}", config.Tag)
	controllerCode = strings.ReplaceAll(controllerCode, "{service}", utils.ToCamelCase(config.Table)+"Service")
	controllerCode = strings.ReplaceAll(controllerCode, "{ApiType}", utils.ToPascalCase(config.ApiType))
	controllerCode = strings.ReplaceAll(controllerCode, "{Api}", config.Api)

	return []string{controllerCode}
}

// 服务构建函数 - 删除
func serviceBuildDelete(config config.Config) []string {
	serviceTemplate := `
/**
 * 删除 {Name} 服务
 * 
 * @param id {Name} ID
 * @return 影响行数
 */
public int delete{Name}{tag}(Long id);
`
	serviceCode := strings.ReplaceAll(serviceTemplate, "{Name}", utils.LineToPascalCase(config.Table))
	serviceCode = strings.ReplaceAll(serviceCode, "{tag}", config.Tag)

	return []string{serviceCode}
}

// 服务实现构建函数 - 删除
func serviceImplBuildDelete(config config.Config) []string {
	serviceImplTemplate := `
// {Name}服务实现	
@Override
public int delete{Name}{tag}(Long id) {
    return {mapper}.delete{Name}{tag}(id);
}
`
	serviceImplCode := strings.ReplaceAll(serviceImplTemplate, "{Name}", utils.LineToPascalCase(config.Table))
	serviceImplCode = strings.ReplaceAll(serviceImplCode, "{tag}", config.Tag)
	serviceImplCode = strings.ReplaceAll(serviceImplCode, "{mapper}", utils.ToCamelCase(config.Table)+"Mapper")

	return []string{serviceImplCode}
}

// Mapper构建函数 - 删除
func mapperBuildDelete(config config.Config) []string {
	mapperTemplate := `
// Mapper
/**
 * 删除 {Name}
 * 
 * @param id {Name} ID
 * @return 影响行数
 */
public int delete{Name}{tag}(Long id);
`
	mapperCode := strings.ReplaceAll(mapperTemplate, "{Name}", utils.LineToPascalCase(config.Table))
	mapperCode = strings.ReplaceAll(mapperCode, "{tag}", config.Tag)

	return []string{mapperCode}
}

// Mapper XML构建函数 - 删除
func mapperXMLBuildDelete(config config.Config) []string {
	mapperXMLTemplate := `
<delete id="delete{Name}{tag}" parameterType="Long">
    DELETE FROM {table} WHERE id = #{id}
</delete>
`
	mapperXMLCode := strings.ReplaceAll(mapperXMLTemplate, "{Name}", utils.LineToPascalCase(config.Table))
	mapperXMLCode = strings.ReplaceAll(mapperXMLCode, "{tag}", config.Tag)
	mapperXMLCode = strings.ReplaceAll(mapperXMLCode, "{table}", config.Table)

	return []string{mapperXMLCode}
}
